﻿function SubtractASecond(days, hours, minutes, seconds) {

	seconds = document.getElementById(seconds);
	minutes = document.getElementById(minutes);
	hours = document.getElementById(hours);
	days = document.getElementById(days);

	if (!seconds || !minutes || !hours || !days)
		return;

	var secondsValue = seconds.firstChild.nodeValue;
    var minutesValue = minutes.firstChild.nodeValue;
    var hoursValue = hours.firstChild.nodeValue;
    var daysValue = days.firstChild.nodeValue;

    if (secondsValue == 0 && minutesValue == 0 && hoursValue == 0 && daysValue == 0)
    {
        return;
    }

    if (secondsValue == 0)
    {
        secondsValue = 60;
        if (minutesValue == 0)
        {
            minutesValue = 60;
            if (hoursValue == 0)
            {
                hoursValue = 24;
                if (daysValue > 0)
                {
                    daysValue--;
                    days.replaceChild(document.createTextNode(MakeTwoDigits(daysValue)), days.firstChild);
                }
            }
            hoursValue--;
            hours.replaceChild(document.createTextNode(MakeTwoDigits(hoursValue)), hours.firstChild);
        }
        minutesValue--;
        minutes.replaceChild(document.createTextNode(MakeTwoDigits(minutesValue)), minutes.firstChild);
    }

    secondsValue--;
    seconds.replaceChild(document.createTextNode(MakeTwoDigits(secondsValue)), seconds.firstChild);

}

function MakeTwoDigits(value)
{
    if (value < 10)
    {
        return "0".concat(value);
    }
    return value;
}